var adedanha = new Adedanha();

// Show the letter
function changeLetter() {
    var letter = adedanha.changeLetter();

    if ( adedanha.state == STARTED ) {
        document.getElementById('letter').innerHTML = letter;
    }
    else if ( adedanha.state == STOPPED ) {
        document.getElementById('message').style.visibility = 'hidden';
        document.getElementById('letter').innerHTML = 'Acabou';
    }
}

window.onload = function() {
    document.getElementById('message').style.visibility = 'hidden';
    document.getElementById('start').old_value
        = document.getElementById('start').value;

    document.getElementById('letter').onclick = function() {
        this.blur();
        changeLetter();
    };

    document.onkeyup = function(e) {
        var keynum = e ? e.which : event.keyCode;
        if ( keynum == 32 ) {
            changeLetter()
        }
    };

    document.getElementById('start').onclick = function() {
        this.blur();
        adedanha.reset();
        adedanha.start();

        document.getElementById('start').value = 'Reiniciar';
        document.getElementById('message').style.visibility = 'visible';
        document.getElementById('letter').innerHTML = 'Clique';
    };

    document.getElementById('stop').onclick = function() {
        this.blur();
        adedanha.stop();
        adedanha.reset();

        document.getElementById('start').value
            = document.getElementById('start').old_value;

        document.getElementById('message').style.visibility = 'hidden';
        document.getElementById('letter').innerHTML = '?';
    };
};
